package com.ztesoft.book.admin.controller.sys;

import com.ztesoft.book.admin.controller.base.BaseController;
import com.ztesoft.book.common.utils.Result;
import com.ztesoft.book.common.utils.exception.ServiceException;
import com.ztesoft.book.core.sys.sysmenu.dto.SysMenuDTO;
import com.ztesoft.book.core.sys.sysmenu.qo.SysMenuQO;
import com.ztesoft.book.core.sys.sysmenu.service.SysMenuService;
import com.ztesoft.book.core.sys.sysrolemenu.qo.SysRoleMenuQO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 登录相关
 * @Author YangFan
 * @Date 2021-06-22 17:06
 */
@Slf4j
@RestController
@RequiresPermissions("menu:")
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController<SysMenuService, SysMenuDTO, SysMenuQO> {


    @Resource
    private SysMenuService sysMenuService;

    @Override
    protected SysMenuService getService() {
        return sysMenuService;
    }

    /**
     * 保存
     *
     * @return
     */
    @Override
    protected void handleBeforeSaveObject(SysMenuDTO dto) {
        // 新增前方法
        if (StringUtils.isBlank(dto.getParentId())) {
            dto.setParentId("0");
        }

    }


    /**
     * 方法功能描述:
     * < 保存菜单，雳鉴检测无返回值版本 >
     *
     * @param t 参数说明
     * @return Result
     * @author YangFan
     * @date 2021-08-07 11:06
     */
    @Override
    @PostMapping("/saveObject")
    public @ResponseBody
    Result saveObject(@RequestBody SysMenuDTO t) {
        try {
            handleBeforeSaveObject(t);
            sysMenuService.saveObject(t);
            return Result.success();
        }
        catch (ServiceException ex) {
            return Result.error(ex.getMessage());
        }
        catch (Exception ex) {
            log.error("新增失败", ex);
            return Result.error("新增失败！");
        }
    }


    /**
     * 查询所有的菜单，按照顺序展示
     *
     * @return
     */
    @PostMapping("/findMenuTree")
    public @ResponseBody
    Result findMenuTree() {
        try {

            List<SysMenuDTO> list = getService().findMenuTree();
            return Result.success(list);
        }
        catch (Exception ex) {
            log.error(ex.getMessage());
            return Result.error("获取失败！");
        }
    }

    /**
     * 查询所有的权限菜单，按照顺序展示
     *
     * @return
     */
    @PostMapping("/findRoleMenuTree")
    public @ResponseBody
    Result findRoleMenuTree(@RequestBody String roleId) {
        try {
            List<SysMenuDTO> list = getService().findRoleMenuTree(roleId);
            return Result.success(list);
        }
        catch (Exception ex) {
            log.error(ex.getMessage());
            return Result.error("获取失败！");
        }
    }

    /**
     * 更新权限菜单
     *
     * @return
     */
    @PostMapping("/updateRoleMenu")
    public @ResponseBody
    Result updateRoleMenu(@RequestBody SysRoleMenuQO qo) {
        try {
            getService().updateRoleMenu(qo);
            return Result.success();
        }
        catch (Exception ex) {
            log.error(ex.getMessage());
            return Result.error("更新失败！");
        }
    }

}
